/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.isfd.util;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.isfd.constant.enums.PartFileTypeEnum;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatcher {

    /**
     * 根据零件截取出零件编号 和 版本
     * 比如吧：
     * 5231C10013G71_A.bdf --> 5231C10013G71 A
     * R_5231C10000G21_B1.bdf --> R_5231C10000G21 B1
     */
    public static String[] getPartNumber(String partName) {
        String partNumber = StringUtils.substringBeforeLast(partName, ".");
        String versionNumber = StringUtils.substringAfterLast(partNumber, "_");
        String[] partSplit = partNumber.split("_");
        if (partNumber.startsWith("R_") && partSplit.length == 3) {
            partNumber = partSplit[0] + "_" + partSplit[1];
            return new String[]{partNumber, versionNumber};
        }

        if (StringUtils.isNotBlank(versionNumber) && versionNumber.length() == 1 && isUpperCaseLetter(versionNumber.charAt(0))) {
            partNumber = StringUtils.substringBeforeLast(partNumber, "_");
        }

        return new String[]{partNumber, versionNumber};
    }

    public static boolean isUpperCaseLetter(char c) {
        return Character.isUpperCase(c) && c >= 'A' && c <= 'Z';
    }

    /**
     * 判断文件类型
     * 如果 文件内容包含 include  则是索引文件
     * 如果 文件名是否包含R_开头 则是紧固件
     * 如果 文件名是否是Gxx或者Gxx.x结尾（x代表数字） 则是零件
     * 都不满足就是 外置文件
     */
    public static PartFileTypeEnum matchesPartFileType(String input, List<String> resultList) {
        if (CollectionUtils.isNotEmpty(resultList)) {
            return PartFileTypeEnum.INDEX;
        }

        if (input.startsWith("R_")) {
            return PartFileTypeEnum.FASTENER;
        }

        String patternString = "^.*G\\d{2}(\\.\\d+)?$";
        Pattern pattern = Pattern.compile(patternString);
        Matcher matcher = pattern.matcher(input);
        if (matcher.matches()) {
            return PartFileTypeEnum.PART;
        }

        return PartFileTypeEnum.EXTERNAL;
    }

    /**
     * 拼接文件存储位置的全路径
     * 比如：/data/model/management/part/data/${批次编号}/C919_10002/C919_S11/5319C97000G22/5319C97003G71_A.bdf
     */
    public static String getDataPath(String prePath, String batchNumber, String indexPath) {
        return prePath + batchNumber + "/" + indexPath;
    }

    /**
     * 把架次好加进去
     * 比如：/data/model/management/part/data/${批次编号}/C919/C919_S11/5319C97000G22/5319C97003G71_A.bdf
     * -->
     * 比如：/data/model/management/part/data/${批次编号}/C919_10002/C919_S11/5319C97000G22/5319C97003G71_A.bdf
     */
    public static String rebuildOldDataPath(String prePath, String batchNumber, String indexPath, String sortie) {
        String[] indexPaths = indexPath.split("/");
        StringBuilder newIndexPath = new StringBuilder();
        for (int i = 0; i < indexPaths.length; i++) {
            if (i == 0) {
                newIndexPath.append(indexPaths[i]);
                if (StringUtils.isNotBlank(sortie)) {
                    newIndexPath.append("_").append(sortie);
                }
            } else {
                newIndexPath.append("/").append(indexPaths[i]);
            }
        }
        return prePath + batchNumber + "/" + newIndexPath;
    }

    /**
     * 拼接文件前缀存储位置
     * 比如：
     * /data/model/management/part/data/${批次编号}
     * /data/model/management/part/temp/${批次编号}
     */
    public static String getDataPrePath(String prePath, String batchNumber) {
        return prePath + batchNumber;
    }

    /**
     * 删除indexPath 开头的batchNumber
     */
    public static String removeBatchNumberInPath(String indexPath, String batchNumber) {
        if (indexPath.startsWith(batchNumber)) {
            return indexPath.replace(batchNumber + "/", "");
        }
        return indexPath;
    }

    /**
     * 重构indexPath
     * 1.删除批次号
     * 2.机型位置重构，去除架次
     * 比如：
     * batch1/C919_10002/C919_10002_D20200910.bdf
     * batch1/C919_10002/C919_S50/C919_S50_D20200514.bdf
     * batch1/C919_10002/C919_S20/5346C11000G22/5346C11011G70_C.bdf
     * C919
     * -->
     * C919/C919_10002_D20200910.bdf
     * C919/C919_S50/C919_S50_D20200514.bdf
     * C919/C919_S20/5346C11000G22/5346C11011G70_C.bdf
     */
    public static String reBuildIndexPath(String indexPath, String modelNumber) {

        String[] indexPaths = indexPath.split("/");
        StringBuilder newIndexPath = new StringBuilder();
        newIndexPath.append(modelNumber);
        for (int i = 0; i < indexPaths.length; i++) {
            if (i == 0 || i == 1) {
                continue;
            }
            newIndexPath.append("/").append(indexPaths[i]);
        }
        return newIndexPath.toString();
    }

    /**
     * 根据机型索引索引和机型编码切分出架次字段
     * 比如：
     * batch1/C919_10002/C919_10002_D20200910.bdf
     * C919
     * --> 10002
     */
    public static String getSortie(String modelIndexPath, String modelNumber) {
        String[] modelIndexPaths = modelIndexPath.split("/");
        String oldModelNumber = modelIndexPaths[1];
        if (oldModelNumber.contains(modelNumber)) {
            String temp = oldModelNumber.replaceAll(modelNumber, "");
            return temp.replaceAll("_", "");
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(getSortie("batch1/C919_10002/C919_10002_D20200910.bdf", "C919"));

        System.out.println(reBuildIndexPath("batch1/C919_10002/C919_10002_D20200910.bdf", "C919"));

        System.out.println(reBuildIndexPath("batch1/C919_10002/C919_S50/C919_S50_D20200514.bdf", "C919"));

        System.out.println(reBuildIndexPath("batch1/C919_10002/C919_S20/5346C11000G22/5346C11011G70_C.bdf", "C919"));

        System.out.println(rebuildOldDataPath("/data/model/management/part/data/", "batch1", "C919/C919_S11/5319C97000G22/5319C97003G71_A.bdf", "10002"));
//        String str1 = "5231C30033G70.1_B.bdf";
//        String[] partNumber = getPartNumber(str1);
//        System.out.println(partNumber[0] + "===" + partNumber[1]);
//
//        System.out.println(matchesPartFileType(partNumber[0],null));
//
//
//        String str2 = "5231C30033G70_B.bdf";
//        String[] partNumber2 = getPartNumber(str2);
//        System.out.println(partNumber2[0] + "===" + partNumber2[1]);
//
//        System.out.println(matchesPartFileType(partNumber2[0],null));


//        String str3 = "5231C30033G70.1_B.bdf";
//        String[] partNumber3 = getPartNumber(str3);
//        System.out.println(partNumber3[0] + "===" + partNumber3[1]);
//
//        System.out.println(matchesPartFileType(partNumber3[0], null));
//
//        String str4 = "5211C01001G71_C.bdf";
//        String[] partNumber4 = getPartNumber(str4);
//        System.out.println(partNumber4[0] + "===" + partNumber4[1]);
//
//        System.out.println(matchesPartFileType(partNumber4[0], null));
//        System.out.println(removeBatchNumberInPath("batch1/C919_10002/C919_S12/C919_S12_D2020624.bdf", "batch1"));
    }
}
